home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1995 November / EnigmA AMIGA RUN 02 (1995)(G.R. Edizioni)(IT)[!][issue 1995-11][Skylink CD].iso / earcd / ead / ead16.dms / ead16.adf / Listati / Friends.c < prev    next >
C/C++ Source or Header  |  1989-06-14  |  6KB  |  183 lines

  1. /* ******************************************************************
  2.  
  3. Programma .... Friends.c
  4. Versione ..... 1.0I, Giugno 1989
  5. Autore ....... Michael Warner
  6. Scopo ........ Effetto grafico di animazione sul puntatore mouse
  7. Software ..... Lattice C V5.02 od Aztec C V3.6a
  8. Hardware ..... Amiga 512K + Kickstart V1.2/V1.3
  9. Sintassi ..... Friends velocità casualità (solo da CLI, opzionali)
  10. Note ......... Traduzione ed adattamento x EAD di Luigi Callegari
  11.  
  12. ****************************************************************** */
  13.  
  14. #include <exec/types.h>
  15. #include <exec/memory.h>
  16. #include <graphics/sprite.h>
  17. #include <graphics/gfxbase.h>
  18. #include <intuition/intuition.h>
  19. #include <intuition/intuitionbase.h>
  20. #include <ctype.h>
  21.  
  22. #ifdef LATTICE           /* Inclusioni per Lattice C V4/5 */
  23. #include <proto/exec.h>
  24. #include <proto/intuition.h>
  25. #include <proto/graphics.h>
  26. #include <proto/dos.h>
  27. #include <string.h>
  28. #include <stdlib.h>
  29. #else
  30. #include <functions.h>   /* Inclusione per Aztec C */
  31. #endif
  32.  
  33. /* Alcune definizioni di costanti di comodo */
  34.  
  35. #define AllocChip(dime) AllocMem((LONG)dime,(MEMF_CHIP|MEMF_CLEAR))
  36. #define DataBytes       (height*2+4)*2
  37. #define prec 8
  38.  
  39. /* Strutture e variabili globali */
  40.  
  41. struct IntuitionBase *IntuitionBase;
  42. struct GfxBase *GfxBase;
  43. struct NewWindow newwin =
  44. { 0, 30, 160, 10, -1, -1,
  45.   CLOSEWINDOW, WINDOWCLOSE | WINDOWDRAG | WINDOWDEPTH | 
  46.   SMART_REFRESH,  NULL, NULL, (UBYTE *)" Friends!  ", NULL, NULL,
  47.   0, 0, 0, 0, WBENCHSCREEN };
  48.  
  49. struct IntuiMessage *msg;
  50. struct Window *win;
  51. struct Screen *wb;            /* schermo workbench */
  52. struct ViewPort *vp;          /* viewport dello screen */
  53. USHORT *clrs;                 /* tabella colori della viewport */
  54. struct SimpleSprite ss[8];    /* usa 1 a 7 */
  55. struct SimpleSprite *ptr;     /* copia del puntatore */
  56. SHORT height;                 /* altezza puntatre */
  57. SHORT which[8]                /* sprite in uso */
  58.   = { 0,0,0,0,0,0,0,0 };    
  59. LONG px[8],py[8];             /* posizioni sprites */
  60. LONG rate = 100;              /* fattore di scala per movimento */
  61. LONG rnd = 100;               /* fattore casualità */
  62.  
  63. LONG fixmult( a, b)             /* Moltiplicazione fissa */
  64. LONG a,b;
  65. {
  66.  return( a >> (prec/2) * b >> (prec/2) );
  67. }
  68.  
  69. LONG tofix(a)        /* Conversione short in n. fisso */
  70. SHORT a;
  71. {
  72.  return( ( (long) a ) << ( prec / 2 ) );
  73. }
  74.  
  75. SHORT toint(a)       /* Converione n.fisso in short */
  76. LONG a;
  77. {
  78.  return( (short)(a >> (prec/2)) );
  79. }
  80.  
  81. void init()    /* Inizializzazione sprites */
  82. {
  83.   register SHORT i, any;
  84.   
  85.   IntuitionBase = (struct IntuitionBase *) \
  86.                                   OpenLibrary("intuition.library",33L);
  87.   GfxBase = (struct GfxBase *)OpenLibrary("graphics.library",33L);
  88.   wb = IntuitionBase -> ActiveWindow -> WScreen;
  89.   vp = &wb -> ViewPort;
  90.   clrs = (USHORT *)(vp -> ColorMap -> ColorTable);/* base tabella colori */
  91.   ptr = *GfxBase -> SimpleSprites;                /* puntatore a ptr */
  92.   height = ptr -> height;                         /* altezza puntatore */
  93.  
  94.   any = FALSE;
  95.   for (i=1; i<8; i++) /* Inizializza sprites */
  96.   {
  97.     if (any |= which[i] = ~GetSprite(&ss[i],(long)i))
  98.     {
  99.       ss[i].height = height;
  100.       
  101.       if (!(ss[i].posctldata = AllocChip(DataBytes)))
  102.          _exit(0);
  103.  
  104.       movmem((char *)ptr->posctldata, (char *)ss[i].posctldata, DataBytes);
  105.       
  106.       ss[i].posctldata[1] = ss[i].posctldata[0] = 0;
  107.       ss[i].posctldata[DataBytes/2-2] = ss[i].posctldata[DataBytes/2-1] \
  108.                                       = 0xffff;
  109.       
  110.       px[i] = tofix(ss[i].x = (short)RangeRand(640L)); /* Posizione */
  111.       py[i] = tofix(ss[i].y = (short)RangeRand(200L));
  112.     }
  113.   }
  114.   if ( !any ) _exit(0);
  115.  
  116.   for ( i=1 ; i<=3; i++) /* Fissa sprites allo stesso colore */
  117.     movmem((char *)&clrs[16],(char *)&clrs[16+i*4],4*2);
  118.   LoadRGB4( vp, clrs, 32L );
  119.   if ( ! (win= OpenWindow( &newwin ) )) _exit(0L); /* Apre finestra */
  120. }
  121.  
  122. void move()        /* muove tutti gli amici del mouse */
  123. {
  124.   static LONG vx[8] = { 0,0,0,0,0,0,0,0 };
  125.   static LONG vy[8] = { 0,0,0,0,0,0,0,0 };
  126.   LONG ax,ay;           /* (punto fisso) accelerazione x ed y */
  127.   LONG mx,my;           /* punto fisso (x,y) mouse */
  128.   register SHORT i;
  129.  
  130.   mx = tofix( wb -> MouseX ); my = tofix( wb -> MouseY );
  131.   for ( i = 1 ; i < 8; i++ )
  132.   {
  133.     if (which[ i ] )
  134.     {
  135.       ax = (mx-px[i]) - 2*vx[i] + tofix((short)(RangeRand(3*rnd)-3*rnd/2));
  136.       ay = (my-py[i]) - 2*vy[i] + tofix((short)(RangeRand(rnd)-rnd/2));
  137.       vx[i] += ax/rate;
  138.       vy[i] += ay/rate;
  139.       px[i] += vx[i]; py[i] += vy[i]; /* Posizione */
  140.       if (toint(px[i])<0)   { px[i]=-px[i]; vx[i]=-vx[i]; }
  141.       if (toint(px[i])>639) { px[i]=tofix(639*2)-px[i]; vx[i]=-vx[i]; }
  142.       if (toint(py[i])<0)   { py[i]=-py[i]; vy[i]=-vy[i]; }
  143.       if (toint(py[i])>199) { py[i]=tofix(199*2)-py[i]; vy[i]=-vy[i]; }
  144.       MoveSprite(vp,&ss[i],
  145.                  (long)toint(px[i]),
  146.                  (long)toint(py[i]));
  147.     }
  148.   }
  149. }
  150.  
  151. void main( argc, argv)
  152. int argc; char *argv[];
  153. {
  154.   register SHORT i;
  155.  
  156.   if (argv[1]) /* legge velocità se specificata */
  157.   {
  158.     rate = atol(argv[1]);
  159.     if (argv[2]) rnd  = atol( argv[2] ); /* Ricava casualità */
  160.   }
  161.   
  162.   init();
  163.   while ( ! ( msg=(struct IntuiMessage *) GetMsg (win->UserPort)))
  164.   {
  165.     WaitTOF();
  166.     move();
  167.   }
  168.   ReplyMsg((struct Message *)msg);
  169.  
  170.   for (i = 1; i < 8; i++)
  171.   {
  172.     if (which[i]) /* Libera memoria allocata da AllocMem() */
  173.     {
  174.       FreeSprite((long)i);
  175.       if (ss[i].posctldata)
  176.         FreeMem(ss[i].posctldata,(long)DataBytes);
  177.     }
  178.   }
  179.   CloseWindow( win );
  180.   CloseLibrary((struct Library *)GfxBase);
  181.   CloseLibrary((struct Library *)IntuitionBase);
  182. }
  183.